package com.localjoycommunity.cpdj.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.localjoycommunity.cpdj.project.system.domain.*;
import com.localjoycommunity.cpdj.project.system.mapper.UserCourseMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserCourseWatchMapper;
import com.localjoycommunity.cpdj.project.system.mapper.UserMapper;
import com.localjoycommunity.cpdj.project.system.service.ISysCourseService;
import com.localjoycommunity.cpdj.project.system.service.IUserCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserCourseServiceImpl extends ServiceImpl<UserCourseMapper, UserCourse> implements IUserCourseService {

    @Autowired
    private ISysCourseService sysCourseService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserCourseWatchMapper userCourseWatchMapper;

    @Override
    public int courseDig(UserCourse userCourse) {
        // 1. 先查询
        UserCourse one = baseMapper.selectOne(new QueryWrapper<UserCourse>().lambda().eq(UserCourse::getUserId, userCourse.getUserId())
                .eq(UserCourse::getCourseId, userCourse.getCourseId()));
        if (one == null) {
            userCourse.setIsDig(1);
            sysCourseService.updateDigNum(userCourse.getCourseId(), 1);
            return baseMapper.insert(userCourse);
        }
        User user = userMapper.selectUserById(userCourse.getUserId());
        Long collectNum = user.getCollectNum() == null ? 0 : user.getCollectNum();
        one.setIsDig(one.getIsDig() ^ 1);
        if (one.getIsDig() == 1) {
            sysCourseService.updateDigNum(userCourse.getCourseId(), 1);
            user.setCollectNum(collectNum + 1);
        } else {
            sysCourseService.updateDigNum(userCourse.getCourseId(), -1);
            collectNum = collectNum - 1 <= 0 ? 0 : collectNum - 1;
            user.setCollectNum(collectNum);
        }
        userMapper.updateUser(user);
        return baseMapper.updateById(one);
    }

    @Override
    public List<SysCourse> selectUserCourse(Integer userId) {
        List<UserCourseWatch> userCourseWatchList = userCourseWatchMapper.selectList(new QueryWrapper<UserCourseWatch>().lambda().eq(UserCourseWatch::getUserId, userId));
        if (userCourseWatchList == null || userCourseWatchList.size() == 0) {
            return null;
        }
        List<SysCourse> list = new ArrayList<>();
        for (UserCourseWatch userCourse : userCourseWatchList) {
            SysCourse sysCourse = sysCourseService.selectSysCourseByIdApp(userCourse.getCourseId());
            sysCourse.setCreateTime(userCourse.getUpdateTime());
            sysCourse.setWatchProgress(userCourse.getWatchProgress());
            list.add(sysCourse);
        }
        return list;
    }

    @Override
    public Integer isDig(Integer userId, Long courseId) {
        return baseMapper.isDig(userId, courseId).orElse(0);
    }

    @Override
    public synchronized void updateUserWatchNum(Integer userId, Long courseId) {
        UserCourseWatch userCourseWatchDb = userCourseWatchMapper.selectUserCourseWatch(courseId, userId);
        SysCourse sysCourse = sysCourseService.selectSysCourseById(courseId);
        if (userCourseWatchDb != null) {
            userCourseWatchDb.setWatchNum(userCourseWatchDb.getWatchNum() + 1);
            userCourseWatchDb.setUpdateTime(new Date());
            userCourseWatchMapper.updateById(userCourseWatchDb);
        } else {
            UserCourseWatch u = new UserCourseWatch();
            u.setUserId(userId);
            u.setCourseId(courseId);
            u.setCreateTime(new Date());
            u.setUpdateTime(new Date());
            u.setIsComplate(0);
            u.setWatchCurrent(0);
            u.setWatchProgress("0小时:0分钟:0秒");
            u.setAllStudyTime("0小时:0分钟:0秒");
            if (sysCourse != null) {
                u.setCourseName(sysCourse.getTitle());
            }
            userCourseWatchMapper.insert(u);
        }
    }

    @Override
    public void updateUserWatchProgess(UserCourseWatch userCourseWatch) {
        SysCourse sysCourse = sysCourseService.selectSysCourseById(userCourseWatch.getCourseId());
        LambdaQueryWrapper<UserCourseWatch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCourseWatch::getCourseId, userCourseWatch.getCourseId()).eq(UserCourseWatch::getUserId, userCourseWatch.getUserId());
        UserCourseWatch userCourseWatchDb = userCourseWatchMapper.selectOne(wrapper);
        Integer watchCurrent = userCourseWatch.getWatchCurrent();
        Integer courseTime = sysCourse.getCourseTime();
        if (courseTime != null && watchCurrent != null) {
            Integer watchCurrentDb = userCourseWatchDb.getWatchCurrent();
            int h = watchCurrent / 3600;
            int m = (watchCurrent % 3600) / 60;
            int s = (watchCurrent % 3600) % 60;
            if (watchCurrent >= watchCurrentDb) {
                userCourseWatchDb.setWatchCurrent(watchCurrent);
                userCourseWatchDb.setWatchProgress(h + "小时:" + m + "分钟:" + s + "秒");
            }
            int allWatchTime = userCourseWatchDb.getAllWatchTime() + watchCurrent;
            userCourseWatchDb.setAllWatchTime(allWatchTime);
            int h1 = allWatchTime / 3600;
            int m1 = (allWatchTime % 3600) / 60;
            int s1 = (allWatchTime % 3600) % 60;
            userCourseWatchDb.setAllStudyTime(h1 + "小时:" + m1 + "分钟:" + s1 + "秒");
        }
        userCourseWatchDb.setUpdateTime(new Date());
        if (userCourseWatch.getIsComplate() == 1) {
            userCourseWatchDb.setIsComplate(1);
        }
        userCourseWatchMapper.updateById(userCourseWatchDb);
    }
}
